<template>
  <div class="housing-dialog">
    <!-- 房屋信息查询修改弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <div class="parent_dom">
          <div class="left" v-for="(item,index) in form_col" :key="index">
            <el-form-item :label="item.title" :prop="item.prop">
              <el-input v-if="item.type === 'input'" v-model="form[item.prop]" :placeholder="item.placeholder" size="mini" />
              <el-select v-if="item.type === 'select'" v-model="form[item.prop]" :placeholder="item.placeholder" clearable size="mini" style="width:100%">
                <el-option v-for="dict in item.options" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
              <el-date-picker v-if="item.type === 'date'" v-model="form[item.prop]" type="date" size="mini" :placeholder="item.placeholder" value-format="yyyy-MM-dd" style="width:100%">
              </el-date-picker>
            </el-form-item>
          </div>
        </div>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" size="mini">取 消</el-button>
        <el-button type="primary" @click="submitForm" size="mini">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRooms, getRooms, delRooms, addRooms, updateRooms, exportRooms } from "../../../../../api/business/project/rooms";
export default {
  dicts: ['bus_cb_type', 'pm_room_direction_type', 'sys_yes_no'],
  data() {
    return {
      title: '',
      open: false,
      form: {
        address: null,
        cb: null,
        symj: null,
        jzmj: null,
        fjs: null,
        direction: null,
        isWsj: null,
        isChf: null,
        roomNo: null,
        jujd: null
      },
      form_col: [{ title: '地址', prop: 'address', type: 'input', placeholder: '请输入地址', options: [], show: true, disabled: false },
      { title: '产别', prop: 'cb', type: 'select', placeholder: '请选择产别', options: [], show: true, disabled: false },
      { title: '建筑面积', prop: 'jzmj', type: 'input', placeholder: '请输入建筑面积', options: [], show: true, disabled: false },
      { title: '房间数', prop: 'fjs', type: 'input', placeholder: '请输入房间数', options: [], show: true, disabled: false },
      { title: '朝向', prop: 'direction', type: 'select', placeholder: '请选择朝向', options: [], show: true, disabled: false },
      { title: '使用面积', prop: 'symj', type: 'input', placeholder: '请输入使用面积', options: [], show: true, disabled: false },
      { title: '是否带卫生间', prop: 'isWsj', type: 'select', placeholder: '请选择是否带卫生间', options: [], show: true, disabled: false, other: true },
      { title: '是否带厨房', prop: 'isChf', type: 'select', placeholder: '请选择是否带厨房', options: [], show: true, disabled: false, other: true },
      { title: '房间编号', prop: 'roomNo', type: 'input', placeholder: '请输入建筑面积', options: [], show: true, disabled: false },
      { title: '家具家电情况', prop: 'jujd', type: 'input', placeholder: '请输入家具家电情况', options: [], show: true, disabled: false },
      ],
      rules: {}
    }
  },
  mounted() {
    this.getoptions()
  },
  methods: {
    getoptions() {
      this.form_col.forEach(element => {
        element.prop === 'cb' ? element.options = this.dict.type.bus_cb_type : []
        element.prop === 'direction' ? element.options = this.dict.type.pm_room_direction_type : []
        element.other ? element.options = this.dict.type.sys_yes_no : []
      });
    },
    // 表单重置
    reset() {
      this.form = {
        address: null,
        cb: null,
        symj: null,
        jzmj: null,
        fjs: null,
        direction: null,
        isWsj: null,
        isChf: null,
        roomNo: null,
        jujd: null
      };
      this.resetForm("form");
    },
    init(row) {
      this.open = true;
      this.reset()
      if (row) {
        this.title = '修改工程管理系统-房屋信息'
        this.getRoomsDeatil(row)
      } else {
        this.title = '添加工程管理系统-房屋信息'
      }
    },
    /** 获取详情信息 */
    getRoomsDeatil(row) {
      const id = row.id
      getRooms(id).then(response => {
        this.form = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRooms(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$parent.getList()
            });
          } else {
            this.form = {
              address: '邯郸',
              cb: "2",
              symj: 180,
              jzmj: 110,
              fjs: "2",
              direction: "1",
              isWsj: "Y",
              isChf: "Y",
              roomNo: "000005",
              jujd: '可用'
            }
            // console.log(this.form)
            addRooms(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$parent.getList()
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    }
  }
}
</script>

<style lang="scss" scoped>
.parent_dom {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  .left {
    width: 50%;
  }
}
</style>